package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Delete extends JInternalFrame {
	
	Select s;
	Connection conn;
	private JLabel label;
	private JLabel qq;
	private JTextField code;
	private JButton button;
	private JTextField serial;
	private JLabel label_2;
	private JTextField ea;
	private JButton button_1;
	private JLabel result;
	
	public Delete(Connection c) {
		super("Delete",true,true,true,false);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 284, 158);
		this.conn=c;
		getContentPane().setLayout(null);
		getContentPane().add(getLabel());
		getContentPane().add(getQq());
		getContentPane().add(getCode());
		getContentPane().add(getButton());
		getContentPane().add(getSerial());
		getContentPane().add(getLabel_2());
		getContentPane().add(getEa());
		getContentPane().add(getButton_1());
		getContentPane().add(getResult());

	}
	public void setD2(Select s){
		this.s = s;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("시리얼");
			label.setBounds(10, 10, 41, 23);
		}
		return label;
	}
	private JLabel getQq() {
		if (qq == null) {
			qq = new JLabel("코드");
			qq.setBounds(10, 39, 41, 23);
		}
		return qq;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(58, 39, 102, 23);
		}
		return code;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("검색");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						int s=Integer.parseInt(serial.getText());
						
						String sql="Select * from ptbl where serial=?";
						PreparedStatement pstmt= conn.prepareStatement(sql);
						pstmt.setInt(1,s);
						ResultSet rs = pstmt.executeQuery();
						
						if(rs.next()){
							code.setText(rs.getString("code"));
							ea.setText(""+rs.getInt("ea"));
							result.setText("데이터가 입력되었습니다.");
						}else{
							result.setText("데이터가 없습니다.");
						}
						rs.close();
						pstmt.close();
						
					}catch(Exception e){
						e.getMessage();
					}
					
				}
			});
			button.setBounds(172, 10, 66, 29);
		}
		return button;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(58, 10, 102, 23);
		}
		return serial;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("수량");
			label_2.setBounds(10, 71, 41, 23);
		}
		return label_2;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(58, 71, 102, 23);
		}
		return ea;
	}
	public void dele(){
		int s= Integer.parseInt(serial.getText());

		
		String sql= "delete from ptbl where serial=?";
		try{
			PreparedStatement pstmt= conn.prepareStatement(sql);
			pstmt.setInt(1, s);
			
			pstmt.executeUpdate();
			pstmt.close();
			serial.setText("");
			code.setText("");
			ea.setText("");
			
		}catch(Exception ex){
			ex.getMessage();
			
		}
	}
	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("삭제");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					dele();
					s.columnadd();
				
				}
			});
			button_1.setBounds(172, 50, 66, 29);
		}
		return button_1;
	}
	private JLabel getResult() {
		if (result == null) {
			result = new JLabel("검색할 코드를 입력하세요...");
			result.setBounds(10, 100, 250, 23);
		}
		return result;
	}
}
